#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/proc_fs.h>
#include <linux/sched.h>
#include <asm/uaccess.h>

#define STRINGLEN 81
char global_buffer[STRINGLEN];

struct proc_dir_entry *example_dir, *proc_hello_file;

int proc_read_hello(char *page, char **start, off_t off, int count, int *eof, void *data);
int proc_write_hello(struct file *file, const char *buffer, unsigned long count, void *data);




int proc_read_hello(char *page, char **start, off_t off, int count, int *eof,
        void *data)
{
    int len;
    len = sprintf(page, global_buffer); //把global_buffer的内容显示给访问者
    printk("read success : len = %d, buff = %s\n", len, global_buffer);
    return len;
}
int proc_read_hello(struct file *file, char __user *buff, size_t size, loff_t *ppos)

int proc_write_hello(struct file *file, const char *buffer, unsigned long count,
        void *data)
{
    int len;

    if (count = STRINGLEN)
    {
        len = STRINGLEN - 1;
    }
    else
    {
        len = count;
    }

    copy_from_user(global_buffer, buffer, len);
    global_buffer[len] = '\0';
    printk("write success : len = %d, buff = %s\n", len, global_buffer);
    return len;
}

static int __init proc_test_init(void)
{
    example_dir = proc_mkdir("proc_test", NULL);
    printk("Create /proc/proc_test success...\n");
    //hello_file = create_proc_entry("hello", S_IRUGO, example_dir);
    //strcpy(global_buffer, "hello");
    //hello_file->read_proc = proc_read_hello;
    //hello_file->write_proc = proc_write_hello;

    static const struct file_operations pid_fops =
    {
        .owner = THIS_MODULE,
        .read = proc_read_hello,
        .write = proc_write_hello,
    };
    proc_hello_file = proc_create("hello", 0666, example_dir, &pid_fops);

    if(proc_hello_file == NULL)
	{
		printk("Can't create /proc/proc_test/hello\n");
        remove_proc_entry("proc_test", NULL);
	}
    printk("Create /proc/proc_test/hello success...\n");
    return 0;
}

static void __exit proc_test_exit(void)
{
    remove_proc_entry("hello", example_dir);
    remove_proc_entry("proc_test", NULL);
}

module_init(proc_test_init);
module_exit(proc_test_exit);
